using Silvery.GameControls;
using Silvery.Command;
namespace Silvery
{
    public interface IGameAssembly
    {
        /// <summary>
        /// 下载管理者
        /// </summary>
        Loader.ILoaderManager GameLoaderManager { get; }
        /// <summary>
        /// 资源管理者
        /// </summary>
        ResourceManager GameResourceManager { get; }
        HotKeys HotKeys { get; }
    }
    public abstract class GameAssembly : DynamicObject, IGameAssembly
    {
        protected GameAssembly()
        {
            HotKeys = new GameControls.HotKeys();
            HotKeys.IsEnabledHotKeys = true;
            #region 创建指令行操作界面
            CommandBox gamecommandbox = new CommandBox();            
            GameCommands = gamecommandbox;
            this.Children.Add(gamecommandbox);
            gamecommandbox.Visibility = System.Windows.Visibility.Collapsed;
            HotKey commandeschotkey = new HotKey(System.Windows.Input.Key.Escape);
            commandeschotkey.HotKeyActivated += (hotkey) =>
                {
                    if (GameCommands.Visibility == System.Windows.Visibility.Visible)
                        GameCommands.Hide();
                };
            HotKeys.Add("CommandBoxShowHotKey", gamecommandbox.DisplayHotKey);
            HotKeys.Add("CommandBoxEscHotKey", commandeschotkey);

            gamecommandbox.commandInput.LostFocus += new System.Windows.RoutedEventHandler(commandInput_LostFocus);
            gamecommandbox.commandInput.GotFocus += new System.Windows.RoutedEventHandler(commandInput_GotFocus);
            
            #endregion
        }

        void commandInput_GotFocus(object sender, System.Windows.RoutedEventArgs e)
        {
            IsLockHotkeyInput = true;
        }

        void commandInput_LostFocus(object sender, System.Windows.RoutedEventArgs e)
        {
            IsLockHotkeyInput = false;
        }
        /// <summary>
        /// 用来帮助其他的焦点输入造成的快捷键按下问题
        /// 比如说，焦点在某一个TextBox里面的时候
        /// </summary>
        protected bool IsLockHotkeyInput { get; set; }
        /// <summary>
        /// 下载管理器
        /// </summary>
        public Loader.ILoaderManager GameLoaderManager { get { return Loader.LoaderManager.Current; } }
        /// <summary>
        /// 游戏资源管理器
        /// </summary>
        public ResourceManager GameResourceManager { get { return ResourceManager.Current; } }
        /// <summary>
        /// 游戏命令行组件
        /// 默认为Home键调出
        /// </summary>
        public IGameCommandBox GameCommands { get; internal set; }
        /// <summary>
        /// 热键组，可以为主程序添加热键
        /// </summary>
        public HotKeys HotKeys { get; internal set; }
        public override void Dispose()
        {
            Children.Remove(GameCommands as CommandBox);
            GameCommands = null;
            HotKeys.Clear();
            base.Dispose();
        }
    }
}
